const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

let alert = function(msg) {
  wx.showToast({
    title: msg,
    icon: 'none',
    duration: 3000
  })
}

let processSkuInfo = function(data) {
  let skuArrays = [];
  for (let i = 0; i < data.kouweiTags.length; i++) {
    skuArrays.push(data.kouweiTags[i]);
  }
  for (let i = 0; i < data.guigeTags.length; i++) {
    skuArrays.push(data.guigeTags[i]);
  }
  for (let i = 0; i < data.manjianTags.length; i++) {
    skuArrays.push(data.manjianTags[i]);
  }

  if (data.canhefeiTag.price != undefined) {
    skuArrays.push(data.canhefeiTag);
  }

  if (skuArrays.length == 0) {
    return '';
  }

  let jsonstr = JSON.stringify(skuArrays);
  console.log('skus=' + jsonstr);
  return jsonstr;
}

module.exports = {
  formatTime: formatTime,
  alert: alert,
  processSkuInfo: processSkuInfo
}
